﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmProductInByTorr2 : Form
    {
        /// <summary>
        /// 需要的库位数
        /// </summary>
        int SNNum = 0;
        /// <summary>
        /// 实际入的库位数
        /// </summary>
        int count = 0;
        /// <summary>
        /// 推荐的库位数据
        /// </summary>
        DataTable dt;
        string realLocationSN;
        string recomLocationSN;

        string StorageSN;
        string AresSN;
        string ProductNumber;


        public FrmProductInByTorr2(string billNo, string standardType, string storageSN, string areaSN, int snNum)
        {
            InitializeComponent();

            lab_BillNo.Text = billNo;
            ProductNumber = Bll_ProductIn.GetProductNumberByBillNo(billNo);
            lab_SNType.Text = standardType;
            SNNum = snNum;
            StorageSN = storageSN;
            AresSN = areaSN;

        }

        private void FrmProductByTorr2_Load(object sender, EventArgs e)
        {
            Bll_ProductIn.UpdateStatuFlag();
            dt = Bll_ProductIn.GetStandardInfoListTop(StorageSN, AresSN, lab_SNType.Text, SNNum - count);
            GetRecommendSN();

            var info = Bll_ProductIn.GetStorageConfirmInfo(lab_BillNo.Text);
            var planQty = 0;
            var realQty = 0;
            if (info.Rows.Count > 0)
            {

                if (!string.IsNullOrEmpty(info.Rows[0]["planQty"].ToString()))
                {
                    planQty = Convert.ToInt32(info.Rows[0]["planQty"]);
                }
                if (!string.IsNullOrEmpty(info.Rows[0]["realQty"].ToString()))
                {
                    realQty = Convert.ToInt32(info.Rows[0]["realQty"]);
                }
            }
            if (SNNum == 1)
            {
                txt_Qty.Text = planQty.ToString();
            }

            lab_msg.Text = "料号:" + ProductNumber
                + "\r\n已完成数量/计划数量:" + realQty + "/" + planQty
                + "\r\n已完成库位/总收货库位:" + count + "/" + SNNum;

        }

        public void GetRecommendSN()
        {
            var result = string.Empty;
            if (dt.Rows.Count > 0)
            {
                var locationSN = dt.Rows[0]["Location_SN"].ToString();
                recomLocationSN = locationSN;
                var arry = locationSN.Split('#');
                if (arry.Count() >= 2)
                {
                    result = arry[1] + arry[2];
                }
                dt.Rows.RemoveAt(0);
            }
            else
            {
                MessageBox.Show("没有推荐库位，请检查" + lab_SNType.Text + "类型的库位是否存在！");
                txt_LocationSN.Focus();
                txt_LocationSN.Text = "";
                txt_Qty.Text = "";
            }

            lab_Recom.Text = result;
        }

        private void btn_ResRecom_Click(object sender, EventArgs e)
        {
            Bll_ProductIn.UpdateStatuFlag();
            dt = Bll_ProductIn.GetStandardInfoListTop(StorageSN, AresSN, lab_SNType.Text, SNNum - count);
            GetRecommendSN();
            txt_LocationSN.Text = "";
            txt_LocationSN.Focus();
        }

        private void txt_Qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (lab_SNType.Text.StartsWith("FG"))
                {
                    if (!recomLocationSN.Equals(realLocationSN))
                    {
                        //lab_msg.Text = "输入的库位与推荐的库位不一致，请重新输入!";
                        MessageBox.Show("输入的库位与推荐的库位不一致，请重新输入!");
                        return;
                    }
                }

                if (string.IsNullOrEmpty(txt_Qty.Text))
                {
                    //lab_msg.Text = "产品数量不能为空!";
                    MessageBox.Show("输入产品数量不能为空!");

                    return;
                }
                txt_Qty.Text = txt_Qty.Text.Split('-').LastOrDefault();

                var qty = Convert.ToInt32(Convert.ToDecimal(txt_Qty.Text));
                var planQty = 0;
                var realQty = 0;

                var info = Bll_ProductIn.GetStorageConfirmInfo(lab_BillNo.Text);
                if (info.Rows.Count > 0)
                {

                    if (!string.IsNullOrEmpty(info.Rows[0]["planQty"].ToString()))
                    {
                        planQty = Convert.ToInt32(info.Rows[0]["planQty"]);
                    }
                    if (!string.IsNullOrEmpty(info.Rows[0]["realQty"].ToString()))
                    {
                        realQty = Convert.ToInt32(info.Rows[0]["realQty"]);
                    }
                    if ((realQty + qty) > planQty)
                    {
                        MessageBox.Show("入库产品总数量" + (realQty + qty) + "不能大于计划数量" + planQty + "!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("单号错误，请退出重试!");
                    return;
                }

                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                //string strTest = lab_BillNo.Text + "_" + realLocationSN + "_" + qty.ToString();

                DialogResult dr = MessageBox.Show("本次操作的入库数量：" + qty + ", 请确认！", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                if (dr == DialogResult.OK)
                {

                    var storageSN = realLocationSN.Split('#')[0];
                    var areaSN = realLocationSN.Split('#')[1];
                    this.txt_Qty.Enabled = false;
                    var result = Bll_ProductIn.ProductInStockByTorr(lab_BillNo.Text, storageSN, storageSN + "#" + areaSN, realLocationSN, qty);
                    this.txt_Qty.Enabled = true;
                    if (result)
                    {
                        count++;
                        lab_msg.Text = "料号:" + ProductNumber
                            + "\r\n已完成数量/计划数量:" + (realQty + qty) + "/" + planQty
                            + "\r\n已完成库位/总收货库位:" + count + "/" + SNNum;
                        if (count == SNNum)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            GetRecommendSN();
                            txt_LocationSN.Text = "";
                            txt_LocationSN.Focus();
                            txt_Qty.Text = "";
                        }
                    }
                    else
                    {
                        //lab_msg.Text = "入库失败!";
                        MessageBox.Show("入库失败!");
                    }

                }

            }
        }

        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    realLocationSN = txt_LocationSN.Text;
                    if (lab_SNType.Text.StartsWith("FG"))
                    {
                        if (!recomLocationSN.Equals(realLocationSN))
                        {
                            //lab_msg.Text = "输入的库位与推荐的库位不一致，请重新输入!";
                            MessageBox.Show("输入的库位与推荐的库位不一致，请重新输入!");
                            txt_LocationSN.Text = "";
                            return;
                        }

                        var status = Bll_ProductIn.GetLocationSNStatus(txt_LocationSN.Text);
                        if (status == 1)
                        {
                            //lab_msg.Text = txt_LocationSN.Text + "已被使用，请点击重新推荐按钮，选择其他推荐库位！";
                            MessageBox.Show("已被使用，请点击重新推荐按钮，选择其他推荐库位!");
                            return;
                        }
                        else if (status == 2)
                        {
                            MessageBox.Show("库位不存在，请检查输入是否正确！");
                            return;
                        }
                        //else
                        //{
                    }
                    else
                    {
                        var status = Bll_ProductIn.GetLocationSNStatus(txt_LocationSN.Text, ProductNumber);
                        if (status == 1)
                        {
                            MessageBox.Show("同一物料不能放在同一库位!");
                            return;
                        }
                    }

                    var arry = txt_LocationSN.Text.Split('#');
                    if (arry.Count() >= 2)
                    {
                        txt_LocationSN.Text = arry[1] + arry[2];
                    }
                    txt_Qty.Focus();
                    //}
                }

                catch (Exception ex)
                {
                    //lab_msg.Text = "输入的库位格式不正确，请重新输入！";
                    MessageBox.Show("输入的库位格式不正确，请重新输入!");
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                }
            }
        }




    }
}